var EnoteModel = require('../models/enoteModel')
var async = require('async');

var enoteCtrl = {};

//
enoteCtrl.get = function (req, res) {


    // var skey = req.query.skey;
    var page = req.query.page || 1;
    var queryCondition = {};
    var sname = req.query.sname;

    if (sname) {
        queryCondition.sname = {
            $regex: new RegExp(sname, "i")
        };
    }

    async.parallel({

        //
        count: function (done) {  // 查询数量
            EnoteModel.count(queryCondition).exec(function (err, count) {
                done(err, count);
            });
        },

        //
        records: function (done) {   // 查询一页的记录
            EnoteModel.find(queryCondition).sort({

                'ctime': -1
            }).skip((page - 1) * 10).limit(10).exec(function (err, docs) {
                done(err, docs);
            });
        },

    }, function (err, results) {

        var count = results.count;
        var records = results.records;
        if (err) {

            res.status(500).json({
                code: -1,
                msg: '没有对应的信息'
            })
        } else {

            res.json({
                code: 0,
                dat: {
                    total: count,
                    records: records
                }
            })
        }

    });
};

//
enoteCtrl.getOne = function (req, res) {

    var _id = req.params.id;
    EnoteModel.findOne({ _id: _id }, function (err, item) {

        if (err) {

            res.status(500).json({
                code: -1,
                msg: '没有对应的信息'
            })
        } else {

            res.json({
                code: 0,
                dat: item
            })
        }

    });

};


//
enoteCtrl.add = function (req, res, next) {

    var data = req.body
    EnoteModel.create(data, function (err, item) {

        if (err) {

            console.log(err)
            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}

//
enoteCtrl.update = function (req, res, next) {

    var data = req.body
    var id = data.id

    EnoteModel.findOneAndUpdate({ id: id }, data, function (err, item) {

        if (err) {

            console.log(err)

            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}

//
enoteCtrl.remove = function (req, res, next) {

    var id = req.body.id;
    var condition = {
        $or: [
            { id: id },
            { parents: id }
        ]
    }

    // console.log('condition = ', condition);
    EnoteModel.deleteMany(condition, function (err) {

        if (err) {

            console.log(err)

            res.status(500).json({
                code: -1,
                msg: '操作失败'
            })
        } else {
            res.json({
                code: 0,

            })
        }

    });

}

module.exports = enoteCtrl;